from utils.viewsets import *
from utils.response import SuccessResponse
from rest_framework import permissions
from rest_framework.response import Response
from apps.HttpTestcase.models import TestsuiteReports
from apps.HttpTestcase.serializers import TestsuiteReportsSerializer
from apps.HttpTestcase.filters import TestsuiteReportsFilter, TestsuiteTestcaseReports
from rest_framework.filters import OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework.generics import DestroyAPIView


class TestsuiteReportsViewSet(ModelViewSet):
    queryset = TestsuiteReports.objects.filter(is_delete=False)
    serializer_class = TestsuiteReportsSerializer

    filter_class = TestsuiteReportsFilter

    # 权限
    permission_classes = [permissions.IsAuthenticated]

    def destroy(self, request, *args, **kwargs):
        data = request.data
        ids = []
        for id in data['id']:
            ids.append(id.get('id'))
        is_de = data.get('is_de')
        if is_de:
            TestsuiteReports.objects.filter(id__in=ids).delete()
            TestsuiteTestcaseReports.objects.filter(testsuite_reports__in=ids).delete()
        else:
            TestsuiteReports.objects.filter(id__in=ids).update(is_delete=True)
        return SuccessResponse(data=None, msg="删除成功！")


class TestsuiteReportsDelete(DestroyAPIView):

    # perms_map = {'delete': 'case_reports_delete'}
    def delete(self, request, *args, **kwargs):
        data = request.data
        ids = []
        for id in data['id']:
            ids.append(id.get('id'))
        is_de = data.get('is_de')
        if is_de:
            TestsuiteReports.objects.filter(id__in=ids).delete()
            TestsuiteTestcaseReports.objects.filter(testsuite_reports__in=ids).delete()
        else:
            TestsuiteReports.objects.filter(id__in=ids).update(is_delete=True)
        return SuccessResponse(data=None, msg="删除成功！")
